home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 436_01 / inintgr.c < prev    next >
Text File  |  1994-10-07  |  6KB  |  278 lines

  1. /*************************************************************************
  2.     Source file:  ININTGR.C
  3.  
  4.     INCON integer input handler.
  5.  
  6.     Compiler:  Borland Turbo C 2.01
  7.  
  8.     INCON source files and the object and library files created from
  9.     them are:
  10.         Copyright (c) 1993-94, Richard Zigler.
  11.     You may freely distribute unmodified source, object, and library
  12.     files, and incorporate them into your own non-commercial software,
  13.     provided that this paragraph and the program name and copyright
  14.     strings defined in INCON.C are included in all copies.
  15. *************************************************************************/
  16.  
  17. #include <conio.h>
  18. #include <ctype.h>
  19. #include <string.h>
  20. #include "indefs.h"                            /* globals and definitions            */
  21. #include "incon.h"                            /* state definitions                    */
  22. #include "indecl.h"                            /* public utility routines            */
  23.  
  24. int pascal hIntegerField( STATES State, register int Pos )
  25. {
  26. register int    s_ptr;
  27. int                max_length    = StrLength - 1,
  28.                     max_width    = Width - 1;
  29.  
  30. switch( State )
  31.     {
  32.     case stError:
  33.         break;
  34.  
  35.     case stQuit:                                /* [Enter] -- end input                */
  36.  
  37.         if ( StrLength < Prec )
  38.             Chr = -1;
  39.         else if ( !(Chr == K_PLUS || Chr == K_MINUS) )
  40.             Chr = 0;
  41.         More = NO;
  42.         break;
  43.  
  44.     case stInit:                                /* field initialization                */
  45.  
  46.         if ( !Flags.Justify )                /* right justify default            */
  47.             Flags.Justify = RJUST;
  48.  
  49.         /****
  50.             If the first character is a sign, force signed input and
  51.             truncate the string at the first occurence of any other
  52.             non-digit character.
  53.         ****/
  54.  
  55.         Pos = 0;
  56.         if ( *OutStr == '+' || *OutStr == '-' )
  57.             {
  58.             Flags.Sign = YES;
  59.             ++Pos;
  60.             }
  61.         while ( Pos < StrLength && isdigit( OutStr[Pos] ) )
  62.             ++Pos;
  63.         OutStr[Pos] = '\0';
  64.         StrLength = Pos;
  65.         Pos = 0;
  66.         return ( Pos );
  67.  
  68.     case stFieldClear:                        /* [Esc] -- clear field or exit    */
  69.  
  70.         if ( EscSet || *OutStr == '\0' )
  71.             More = NO;
  72.         else
  73.             {
  74.             Pos = StrLength = 0;
  75.             *OutStr = '\0';
  76.             ++Update;
  77.             }
  78.         break;
  79.  
  80.     case stDeleteCharLeft:                    /* [Backspace] -- delete left        */
  81.  
  82.         if ( Pos )
  83.             {
  84.             if ( Pos > max_length )
  85.                 {
  86.                 putch( BS );
  87.                 putch( Fill );
  88.                 OutStr[--Pos] = '\0';
  89.                 --StrLength;
  90.                 --Move;
  91.                 }
  92.             else if ( Pos == max_length )
  93.                 goto __TruncateAtCursor;
  94.             else
  95.                 {
  96.                 --Pos;
  97.                 goto __MoveCharsToCursor;
  98.                 }
  99.             }
  100.         break;
  101.  
  102.     case stMoveToStart:                        /* [Home] -- start of field        */
  103.  
  104.         if ( Pos )
  105.             {
  106.             Pos = 0;
  107.             --Move;
  108.             }
  109.         break;
  110.  
  111.     case stMoveCharLeft:                        /* [Left Arrow] -- move char lt    */
  112.  
  113.         if ( Pos )
  114.             {
  115.             --Pos;
  116.             --Move;
  117.             }
  118.         break;
  119.  
  120.     case stMoveCharRight:                    /* [Right Arrow] -- move char rt    */
  121.  
  122.         if ( Pos < StrLength && Pos < max_width )
  123.             {
  124.             ++Pos;
  125.             ++Move;
  126.             }
  127.         break;
  128.  
  129.     case stMoveToEnd:                            /* [End] -- end of field            */
  130.  
  131.         if ( Pos < StrLength && Pos < max_width )
  132.             {
  133.             Pos = StrLength;
  134.             ++Move;
  135.             }
  136.         break;
  137.  
  138.     case stMoveWordLeft:                        /* [Ctrl Left] -- move word lt    */
  139.  
  140.         if ( Pos )
  141.             {
  142.             Pos = WordLeft( Pos, OutStr, ' ' );
  143.             --Move;
  144.             }
  145.         break;
  146.  
  147.     case stMoveWordRight:                    /* [Ctrl Right] -- move word rt    */
  148.  
  149.         if ( Pos < StrLength && Pos < max_width )
  150.             {
  151.             Pos = WordRight( Pos, OutStr, ' ' );
  152.             ++Move;
  153.             }
  154.         break;
  155.  
  156.     case stDeleteWordLeft:                    /* [Ctrl L] -- delete word left    */
  157.  
  158.         if ( Pos )
  159.             {
  160.             s_ptr = Pos;
  161.             Pos = WordLeft( Pos, OutStr, ' ' );
  162.             goto __DeleteWord;
  163.             }
  164.         break;
  165.  
  166.     case stDeleteWordRight:                    /* [Ctrl R] -- delete word right    */
  167.  
  168.         if ( Pos < StrLength )
  169.             {
  170.             s_ptr = WordRight( Pos, OutStr, ' ' );
  171.  
  172. __DeleteWord:
  173. ;
  174.             memcpy( OutStr + Pos, OutStr + s_ptr, StrLength - s_ptr + 1 );
  175.             StrLength -= (s_ptr - Pos);
  176.             ++Update;
  177.             }
  178.         break;
  179.  
  180.     case stDeleteToEnd:                        /* [Ctrl End] -- clear to end        */
  181.  
  182.         if ( Pos < StrLength )
  183.             goto __TruncateAtCursor;
  184.         break;
  185.  
  186.     case stDeleteToStart:                    /* [Ctrl Home] -- clear to start    */
  187.  
  188.         if ( Pos )
  189.             {
  190.             memcpy( OutStr, OutStr + Pos, StrLength - Pos + 1 );
  191.             StrLength -= Pos;
  192.             Pos = 0;
  193.             ++Update;
  194.             }
  195.         break;
  196.  
  197.     case stDeleteAtCursor:                    /* [Del] -- delete at cursor        */
  198.  
  199.         if ( StrLength )
  200.             {
  201.             if ( Pos < max_length )
  202.                 {
  203. __MoveCharsToCursor:
  204. ;
  205.                 memcpy( OutStr + Pos, OutStr + Pos + 1, StrLength - Pos );
  206.                 --StrLength;
  207.                 ++Update;
  208.                 }
  209.             else if ( Pos == max_length )
  210.                 {
  211. __TruncateAtCursor:
  212. ;
  213.                 OutStr[Pos] = '\0';
  214.                 StrLength = Pos;
  215.                 ++Update;
  216.                 }
  217.             }
  218.         break;
  219.  
  220.     case stExitPlus:                            /* [Keypad +] -- special exit        */
  221.     case stExitMinus:                            /* [Keypad -] -- special exit        */
  222.  
  223.         /****
  224.             If cursor is beyond the sign position or Sign flag is off,
  225.             treat key as an exit; otherwise convert Chr to '+' or '-'
  226.             and fall through to the match state.
  227.         ****/
  228.  
  229.         if ( Pos || !Flags.Sign )
  230.             {
  231.             hIntegerField( stQuit, 0 );
  232.             break;
  233.             }
  234.         else
  235.             Chr = (State == stExitPlus) ? '+' : '-' ;
  236.  
  237.     case stCharMatch:                            /* see if entry matches field        */
  238.  
  239.         if
  240.             (
  241.             isdigit( Chr )
  242.             ||
  243.             (!Pos && Flags.Sign && (Chr == '-' || Chr == '+'))
  244.             )
  245.             {
  246.  
  247.             /****
  248.                 If this is the first entry, clear the field and place
  249.                 the new character at the beginning.
  250.             ****/
  251.  
  252.             if ( InBegin )
  253.                 {
  254.                 *(WORD *)OutStr = Chr;        /* put Chr and '\0'                    */
  255.                 Pos = StrLength = 1;
  256.                 }
  257.             else if ( Pos == StrLength )
  258.                 {
  259.                 if ( StrLength >= Width )
  260.                     --StrLength;
  261.                 *(WORD *)(OutStr + Pos++) = Chr;
  262.                 ++StrLength;
  263.                 }
  264.             else
  265.                 OutStr[Pos++] = Chr;
  266.          ++Update;
  267.             }                                        /* if (isdigit)                        */
  268.         else
  269.             State = stError;
  270.         break;
  271.     }                                                /* switch                                */
  272. if ( Pos >= Width )
  273.     Pos = Width - 1;
  274. return( Pos );
  275. }
  276.  
  277. /**** EOF:  ININTGR.C ****/
  278.